package cn.swenty.backstage.sso.server.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
/** 
* @ClassName: OAuthService 
* @Description: TODO(Oatuth2操作) 
* @author sunyang
* @date 2018年1月26日 上午11:03:22 
*  
*/
@Service
public class OAuthService {

    private Cache cache;

    @Autowired
    public OAuthService(CacheManager cacheManager) {
        this.cache = cacheManager.getCache("code-cache");
    }

    public void addAuthCode(String authCode, String username) {
        cache.put(authCode, username);
    }

    public void addAccessToken(String accessToken, String username) {
        cache.put(accessToken, username);
    }

    public String getUsernameByAuthCode(String authCode) {
        return (String)cache.get(authCode).get();
    }

    public String getUsernameByAccessToken(String accessToken) {
        return (String)cache.get(accessToken).get();
    }

    public boolean checkAuthCode(String authCode) {
        return cache.get(authCode) != null;
    }

    public boolean checkAccessToken(String accessToken) {
        return cache.get(accessToken) != null;
    }

    public long getExpireIn() {
        return 3600L;
    }
}
